<template>
    <div class="add">
    <el-row class="head">
        <el-col >——新增维修区域/维修区域</el-col>
    </el-row>
        <el-row class="title">
            <el-col>新增维修区域</el-col>
        </el-row>
        <el-form  :model="form" label-width="100px" class="formrule" :rules="rules" ref="ruleform">
            <el-form-item label="一级区域" prop="first">
                <el-select v-model="form.first" allow-create filterable placeholder="请选择或输入">
                    <el-option
                            v-for="item in firsts"
                            :key="item.value"
                            :label="item.first"
                            :value="item.first"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="二级区域" prop="second">
                <el-input v-model="form.second"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit(form)">添加</el-button>
            </el-form-item>
        </el-form>
        </div>
</template>

<script>
    export default {
        name: "add",
        data() {
            return {
                form:{
                    first:"",
                    second:"",
                },
                firsts:[],
                rules: {
                    first: [
                        {required: true, message: '请输入一级区域', trigger: 'blur'},
                    ],
                      second: [
                            { required: true, message: '请输入二级区域', trigger: 'blur' },
                            { min: 1, max: 20, message: '长度在 1到 20个字符', trigger: 'blur' }
                        ],

                }
            }
        },
        created()
        {
            this.$axios.post("/api/v1/user/seldistinctfirst").then(result => {

                return result.data;
            })
                .then(data1 => {

                    this.firsts=data1.data;
                });
        },
        methods:{
            onSubmit(form)
            {
                this.$refs.ruleform.validate((valid) => {
                    if (valid) {
                        this.$axios.post("/api/v1/user/admin/inslocation",form).then(result =>
                        {
                            return result.data;
                        })
                            .then(data =>{
                                console.log(data.data);
                                this.$message({
                                    type: 'success',
                                    message: data.msg,
                                });
                                this.form.first='';
                                this.form.second='';
                            });
                    } else {
                        return false;
                    }
                });

            }
        }
    }
</script>

<style scoped>
    .el-input{
        width: 600px;
    }
</style>